package com.schoolvisitors.repository;

import com.schoolvisitors.entity.TimeSlot;

import jakarta.persistence.LockModeType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;

import java.time.LocalTime;
import java.util.List;

@Repository
public interface TimeSlotRepository extends JpaRepository<TimeSlot, Long> {

    // 获取重叠的时间段
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("SELECT t FROM TimeSlot t WHERE t.startTime < :endTime AND t.endTime > :startTime AND t.delete = false")
    List<TimeSlot> findOverlappingTimeSlotForUpdate(
            @Param("startTime") LocalTime startTime,
            @Param("endTime") LocalTime endTime);


    @NonNull
    @Query("SELECT t FROM TimeSlot t WHERE t.delete = false")
    List<TimeSlot> findAll();


    List<TimeSlot> findAllByDeleteFalseOrderByStartTimeAsc();

    TimeSlot findAllByDeleteFalse();

    TimeSlot findByIdAndDeleteFalse(Long id);
}
